const Controller = require('../core/base_controller');

const assert = require('assert');

class AppController extends Controller {
  get repair() {
    return 'repair';
  }
  get workflow() {
    return 'repair_workflow';
  }

  async index() {
    const { app } = this;

    const where = this.pick(Object.keys(this.app.model.Repair.attributes));

    const result = await app.table(this.repair).page({
      where,
      order: [
        ['created_at', 'DESC']
      ]
    });

    this.success(result);
  }

  // 详情
  async detail() {
    const { app } = this;

    const result = await app.table(this.repair).findOne({
      where: this.param
    });
    this.success(result);
  }

  // 新增报修
  async add() {
    const { service: { repairWorkflow: workflow } } = this;

    let { user } = this.ctx.state

    const model = this.param;
    model.create_userid = user.userid;
    model.create_username = user.username;
    model.status = 1;

    const data = await this.app.table(this.repair).insert(model);

    // 新建流程记录
    await workflow.add({
      workflow_id: data.id,
      from_userid: user.userid,
      from_username: user.username,
      step: '新建报修',
    });

    this.success(data);
  }

  // 修改问题
  async edit(status, statusText, remark) {
    const { app, ctx, service: { repairWorkflow: workflow } } = this;
    const { model: { Repair } } = app

    let { user } = this.ctx.state


    const param = this.pick(Object.keys(Repair.attributes));
    param.status = status;
    const data = await app.table(this.repair).update(param);

    return data
  }

  // 指派&转发
  async send() {
    let param = this.param
    let statusText;
    if (param.is_zf) {
      statusText = '转发'
    } else {
      statusText = '指派'
    }

    let ret = await this.edit(2);

    const { service: { repairWorkflow: workflow } } = this;
    let { user } = this.ctx.state


    // 新建流程记录
    await workflow.add({
      workflow_id: param.id,
      from_username: user.username,
      form_userid: user.userid,
      to_username: param.do_person_name,
      to_userid: param.to_userid,
      step: statusText,
      remark: param.do_remark,
    });

    this.success(ret)
  }

  // 接收
  async receive() {

    let ret = await this.edit(3);
    let { user } = this.ctx.state

    // 新建流程记录
    let param = this.param
    const { service: { repairWorkflow: workflow } } = this;

    await workflow.add({
      workflow_id: param.id,
      from_username: user.username,
      form_userid: user.userid,
      step: '已接单',
      remark: param.do_remark,
    });

    this.success(ret)

  }

  // 处理
  async do() {
    const { app, ctx, service: { repairWorkflow: workflow } } = this;
    const { model: { Repair } } = app

    const param = this.pick(Object.keys(Repair.attributes));
    let { user } = this.ctx.state

    //已解决
    let step = '未解决'
    if (param.result == '90000000-0000-0000-0000-000000000001') {
      param.status = 4;
      step = '已解决'
    }
    const data = await app.table(this.repair).update(param);

    // 新建流程记录
    await workflow.add({
      workflow_id: param.id,
      from_username: user.username,
      form_userid: user.userid,
      step,
      files: data.files2,
      remark: `原因是:${param.reason}`
    });

    this.success(data);
  }

  // 评价
  async eval() {
    let ret = await this.edit(5);
    let { user } = this.ctx.state

    // 新建流程记录
    let param = this.param
    const { service: { repairWorkflow: workflow } } = this;

    await workflow.add({
      workflow_id: param.id,
      from_username: user.username,
      form_userid: user.userid,
      step: '评价报修',
      remark: param.eval_content,
    });

    this.success(ret)
  }

  // 签到
  async sign() {
    let { user } = this.ctx.state
    const { service: { repairSign }, app: { model: { RepairSign } } } = this
    let param = this.pick(Object.keys(RepairSign.attributes));
    param.userid = user.userid
    param.username = user.username
    let ret = await repairSign.add(param)
    this.success(ret)
  }

  //获取流程信息
  async workflow() {
    let { id } = this.param
    assert(id, '缺少流程Id参数！')

    const { service: { repairWorkflow } } = this
    let ret = await repairWorkflow.getWorkflow(id)
    this.success(ret)
  }
}
module.exports = AppController;
